<template>
  <div class="login-background">
    <div class="title-container">
      <h1 class="main-title">十二相整流发电机虚拟训练软件</h1>
    </div>
    <div class="login-wrapper">
      <div class="login-container">
        <div class="login-form">
          <div class="login-header"></div>
          <el-form
            style="color: white"
            :model="form"
            @submit.prevent="handleLogin"
          >
            <el-form-item label="用户名">
              <el-input
                v-model="form.username"
                placeholder="请输入用户名"
                prefix-icon="User"
                size="large"
                clearable
              />
            </el-form-item>
            <el-form-item label="密码">
              <el-input
                v-model="form.password"
                type="password"
                placeholder="请输入密码"
                prefix-icon="Lock"
                size="large"
                show-password
              />
            </el-form-item>
            <el-form-item>
              <el-button
                type="primary"
                native-type="submit"
                size="large"
                :loading="loading"
                class="login-button"
              >
                登录
              </el-button>
            </el-form-item>
          </el-form>
          <p :class="messageClass">{{ message }}</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive, computed, ref } from "vue";
import { useRouter } from "vue-router";
import { login } from "@/api/util";
import CryptoJS from "crypto-js";
import { useUserStore } from "@/store/user";
import loginBg from "@/assets/login.jpg";

const form = reactive({
  username: "admin",
  password: "800304",
});

const message = ref("");
const messageColor = ref("red");
const loading = ref(false);
const router = useRouter();
const userStore = useUserStore();

const messageClass = computed(() => ({
  message: true,
  success: messageColor.value === "green",
  error: messageColor.value === "red",
}));

const handleLogin = async () => {
  loading.value = true;
  message.value = "";

  try {
    // 使用MD5加密密码
    const md5Password = CryptoJS.MD5(form.password).toString();
    console.log("md5Password:", md5Password);

    // 调用登录接口
    const result = await login({
      account: form.username,
      password: md5Password,
    });

    // 登录成功处理
    message.value = result.message || "登录成功！";
    messageColor.value = "green";

    // 确保登录接口返回的数据结构与verifyLogin一致
    const loginResponse = {
      code: result.code || 100,
      message: result.message || "登录成功",
      data: result.data || result,
    };

    // 使用user store保存登录响应和用户信息
    userStore.login(loginResponse);

    // 跳转到首页
    router.push("/home");
  } catch (error) {
    // 登录失败处理
    message.value = error.message || "登录失败，请重试！";
    messageColor.value = "red";
    console.error("登录错误:", error);
  } finally {
    loading.value = false;
  }
};
</script>

<style scoped>
.login-background {
  min-height: 100vh;
  background-image: url("@/assets/login.jpg");
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  padding: 60px 20px 20px;
}

.title-container {
  z-index: 10;
  margin-bottom: 40px;
}

.main-title {
  text-align: center;
  font-weight: bold;
  font-size: 52px;
  color: white;
  margin: 0;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
}

.login-wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  flex: 1;
}

.login-container {
  width: 100%;
  max-width: 500px;
  background: transparent;
  border-radius: 16px;
  overflow: hidden;
  backdrop-filter: blur(0px);
  animation: slideUp 0.6s ease-out;
}

.login-form {
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h1.main-title {
  font-size: 32px;
  color: white;
  margin-bottom: 20px;
  text-align: center;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
}

.login-header h2 {
  font-size: 28px;
  color: #003366;
  margin-bottom: 10px;
  font-weight: 600;
}

.login-header p {
  font-size: 14px;
  color: #666;
}

:deep(.el-form-item) {
  margin-bottom: 44px;
}

:deep(.el-form-item__label) {
  color: white;
  font-weight: 500;
  width: 80px !important; /* 设置固定宽度使输入框对齐 */
}

:deep(.el-input__wrapper) {
  background: #f5f7fa;
  border-radius: 8px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
}

:deep(.el-input__wrapper:hover) {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  background: #fff;
}

:deep(.el-input__prefix) {
  padding-left: 12px;
}

:deep(.el-input__suffix) {
  padding-right: 12px;
}

.login-button {
  width: 100%;
  margin-top: 10px;
  background: linear-gradient(135deg, #003366 0%, #0066cc 100%);
  border: none;
  border-radius: 8px;
  height: 48px;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 1px;
  transition: all 0.3s ease;
  box-shadow: 0 4px 15px rgba(0, 51, 102, 0.4);
}

.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(0, 51, 102, 0.6);
}

.login-button:active {
  transform: translateY(0);
}

.message {
  text-align: center;
  margin-top: 20px;
  font-size: 14px;
  min-height: 20px;
  transition: all 0.3s ease;
}

.message.success {
  color: #67c23a;
  font-weight: 500;
}

.message.error {
  color: #f56c6c;
  font-weight: 500;
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (max-width: 480px) {
  .login-form {
    padding: 30px 20px;
  }

  .login-header h1 {
    font-size: 24px;
  }
}
</style>
